<template>
  <div class="relative w-full h-full overflow-y-auto">
    <PageHeader :title="t('service.退款详情')">
      <template #right>
        <el-button type="primary" @click="goBack" size="small">
          <SvgIcon name="iconfanhui" color="#fff" class="mr4" />{{
            t("service.返回上级页面")
          }}
        </el-button>
      </template>
    </PageHeader>

    <Step :detail="detailInfo" @rest="getDetail" v-show="detailInfo.id" />

    <template
      v-if="detailInfo.refundAskType == 2 && detailInfo.refundCheckTime"
    >
      <div class="mt15 mb15 pl5 color-333333 font-700">
        {{ t("service.买家退货信息") }}:
      </div>
      <el-table
        :show-header="false"
        :data="sendInfo"
        style="width: 100%"
        :cell-class-name="cellClassName"
        border
        :span-method="arraySpanMethod"
      >
        <el-table-column prop="label1" />
        <el-table-column prop="value1" />
        <el-table-column prop="label2" />
        <el-table-column prop="value2" />
      </el-table>
    </template>

    <div class="mt15 mb15 pl5 color-333333 font-700">
      {{ t("service.退款信息") }}:
    </div>
    <el-table
      :show-header="false"
      :data="tableData"
      style="width: 100%"
      :cell-class-name="cellClassName"
      border
      :span-method="arraySpanMethod"
    >
      <el-table-column prop="label1" />
      <el-table-column prop="value1" />
      <el-table-column prop="label2" />
      <el-table-column prop="value2" />
    </el-table>

    <div class="mt15 mb15 pl5 color-333333 font-700">
      {{ t("service.退款凭证信息") }}:
    </div>
    <el-table
      :show-header="false"
      :data="tableData2"
      style="width: 100%"
      :cell-class-name="cellClassName"
      border
      :span-method="arraySpanMethod2"
    >
      <el-table-column prop="label1" />
      <el-table-column prop="value1">
        <template #default="scope">
          <div class="flex_aic" v-if="scope.row.value1">
            <el-image
              v-for="(img, index) in scope.row.value1.split(',')"
              :key="img"
              style="width: 100px; height: 100px"
              :src="img"
              :zoom-rate="1.2"
              :max-scale="7"
              :min-scale="0.2"
              :preview-src-list="scope.row.value1.split(',')"
              show-progress
              :initial-index="4"
              fit="cover"
              preview-teleported
            />
          </div>
          <span v-else>{{ t("service.暂无凭证") }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="label2" />
      <el-table-column prop="value2" />
    </el-table>

    <div class="mt15 mb15 pl5 color-333333 font-700">
      {{ t("service.商品信息") }}:
    </div>
    <el-table
      :data="detailInfo.saleSkus"
      style="width: 100%"
      border
      class="cover-table"
    >
      <el-table-column :label="t('service.序号')" width="100" type="index" />
      <el-table-column :label="t('service.商品信息')" prop="info">
        <template #default="{ row }">
          <GoodsTableTd :row="row" />
        </template>
      </el-table-column>
      <!-- <el-table-column
        :label="t('service.商品单价')"
        prop="skuStockPrice"
        width="160"
      >
        <template #default="{ row }">
          <span>￥{{ row.skuStockPrice }}</span>
        </template>
      </el-table-column> -->
      <el-table-column
        :label="t('service.退款数量')"
        prop="refundSkuCount"
        width="160"
      />
      <!-- <el-table-column
        :label="t('service.打包费')"
        prop="skuPostage"
        width="160"
        :formatter="(row) => '￥' + row.skuPostage"
      /> -->
      <el-table-column
        :label="$t('order_lists.小料')"
        width="200"
      >
        <template #default="{ row }">
          <div class="border-left-right">
            <div>{{$t('currency.￥')}}{{row.payRealMate}}</div>
            <div class="color-999999">
              {{row.skuMateDetail.map(v=> v['mateName' + locale]).join(',')}}
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        :label="t('service.实付金额')"
        prop="payRealSku"
        width="160"
        :formatter="(row) => '￥' + row.payReal"
      />
      <!-- <el-table-column prop="refundRealSku" label="退款金额(元)" width="160">
        <template #default="{ row }">
          <span>￥{{ row.refundRealSku || "--" }}</span>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- <span class="color-ff2b4e flex justify-end mt12 font-600">
      {{ $t("order_lists_detail.订单实付金额") }}(¥{{ detailInfo.payReal }}) = 
      {{ $t("order_lists.小料金额") }}(¥{{ detailInfo.payRealMate }}) +
      {{ $t("order_lists.菜品金额") }}(¥{{ detailInfo.payRealSku }}) +
      {{ $t("order_lists.小费金额") }}(¥{{ detailInfo.payRealTip }})  +
      {{ $t("order_lists.包装费用") }}(¥{{ detailInfo.payRealPackingFee }})
    </span> -->
  </div>
</template>
<script setup>
import { ref, onMounted, watchEffect } from "vue";
import { useRouter } from "vue-router";
import Api from "@/config/api/api";
import { useRoute } from "vue-router";
import { useI18n } from "vue-i18n";
import Step from "./step.vue";
import moment from "moment";
import GoodsTableTd from "@/components/goodsTableTd.vue";

const { locale, t } = useI18n();
const route = useRoute();
const router = useRouter();
const detailInfo = ref({});

// 退款信息
const tableData = ref([]);
// 退款凭证信息
const tableData2 = ref([]);
// 买家发货信息
const sendInfo = ref([]);
watchEffect(() => {
  tableData.value = [
    {
      label1: t("service.订单号"),
      value1: detailInfo.value.sourceSaleNo,
      label2: t("service.退款编号"),
      value2: detailInfo.value.saleNo,
    },
    {
      label1: t("service.退款金额元"),
      value1: detailInfo.value.refundReal,
      label2: t("service.退款原因"),
      value2: detailInfo.value["refundAskReasonName" + locale.value],
    },
    {
      label1: t("service.会员"),
      value1: detailInfo.value.vipNickName,
      label2: t("service.补充说明"),
      value2: detailInfo.value.refundAskRemark,
    },
  ];

  tableData2.value = [
    {
      label1: t("service.退款凭证"),
      value1: detailInfo.value.refundAskSkuImage,
    },
  ];

  sendInfo.value = [
    {
      label1: t("service.快递单号"),
      value1: detailInfo.value.refundVipSendNo,
      label2: t("service.发货时间"),
      value2: detailInfo.value.refundVipSendTime
        ? moment(detailInfo.value.refundVipSendTime).format(
            "YYYY-MM-DD HH:mm:ss"
          )
        : "",
    },
    {
      label1: t("service.退货地址"),
      value1:
        detailInfo.value["refundCheckAddressProvince" + locale.value] +
        detailInfo.value["refundCheckAddressCity" + locale.value] +
        detailInfo.value["refundCheckAddressArea" + locale.value] +
        detailInfo.value["refundCheckAddressAddress" + locale.value],
      label2: t("service.收货人"),
      value2: detailInfo.value["refundCheckAddressName" + locale.value],
    },
    {
      label1: t("service.联系电话"),
      value1: detailInfo.value.refundCheckAddressPhone,
    },
  ];
});
const cellClassName = ({ row, column, rowIndex, columnIndex }) => {
  if (columnIndex % 2 === 0) {
    return "warning-column winth-20";
  } else {
    return "winth-30";
  }
};

const arraySpanMethod = ({ row, column, rowIndex, columnIndex }) => {
  if (
    (columnIndex === 2 && rowIndex === 3) ||
    (columnIndex === 3 && rowIndex === 3)
  ) {
    return [0, 0];
  } else if (columnIndex === 1 && rowIndex === 3) {
    return [1, 3];
  } else {
    return [1, 1];
  }
};

const arraySpanMethod2 = ({ row, column, rowIndex, columnIndex }) => {
  if (columnIndex === 1 && rowIndex === 0) {
    return [1, 3];
  } else {
    return [1, 1];
  }
};

const valueFormatter = (row, column) => {
  console.log(row, column);
  return detailInfo.value[column.property];
};

const goBack = () => {
  router.back();
};

const getDetail = () => {
  Api.getOrderDetail(route.query.id).then((res) => {
    detailInfo.value = res.data;
  });
};

onMounted(() => {
  getDetail();
});
</script>
<style lang="scss" scoped>
.mt50 {
  margin-top: 50px;
}

:deep(.warning-column) {
  background-color: #f8f9fa;
}
:deep(.winth-20) {
  width: 20%;
}
:deep(.winth-30) {
  width: 30%;
}
.w-380 {
  width: 380px;
}
</style>
